/**
 * 风格设置与导航设置
 */
define(function(require, exports, module) {
    var $ = require("jquery");
    var util = require("util");
    var ui = require("ui");
    var tmpl = require("tmpl");
    require('authorize');
    require("permission");
    var api_prefix = G_CONFIG["api_prefix"] + "/qmyx/style-setting/"; //api前缀
    
    var StyleSetting = {
        token:null,
        styleCode:null,
        init:function(){
            this.token = util.getToken();
            this.bindEvent();
            this.getCurStyle();
        },
        bindEvent:function(){
            var _this = this;
            $("#radio_list input:radio").on("click",function(e){
                var style_code = this.value;
                if(style_code){
                    _this.changeStyle(style_code);
                }
            });
            $("#style_list a").on("click",function(e){
                var style_code = $(this).attr('style-code') || "";
                if(style_code){
                    _this.changeStyle(style_code);
                }
            });
            $("#nav_setting_tbl").on("click",".icon-op-edit",function(e){
                e.preventDefault();
                var $el = $(e.currentTarget);
                var oid = $el.attr("oid") || "";
                var nav_name = $el.attr("nav_name") || "";
                var nav_img_url = $el.attr("nav_img_url") || "";
                _this.editSetting(oid,nav_name,nav_img_url);
            });
            $("#nav_setting_tbl").on("click",".icon-op-restore",function(e){
                e.preventDefault();
                var oid = $(e.currentTarget).attr("oid") || "";
                if(oid){
                    util.confirm("您确定要还原为当前风格默认的图片吗?",{
                        ok:function(){
                            _this.resetNavImg(oid);
                        }
                    });
                }
            });
            $("#nav_setting_tbl").on("mouseenter","tr",function(){
                $(this).find(".js-oper-btn").show();
            });
            $("#nav_setting_tbl").on("mouseleave","tr",function(){
                $(this).find(".js-oper-btn").hide();
            });
        },
        editSetting:function(oid,nav_name,nav_img_url){
            var _this = this;
            if(!oid){
                nav_img_url = "";
            }
            require.async(["dialogplus","uploader"],function(dialog){
                var html = tmpl('upload_tmpl', {oid:oid,nav_name:nav_name,nav_img_url:nav_img_url});
                var d = dialog({
                    title:"编辑",
                    fixed: true,
                    content:html,
                    width:500,
                    height:200,
                    onshow:function(){
                        $('#fileupload').fileupload({
                            dataType: 'json',
                            url:G_CONFIG["api_prefix"] + "common/file/upload-image",
                            add:function(e,data){
                                if(util.checkIsImagFile(data.files[0])){
                                    data.submit();
                                    $("#img_wrap").append('<div class="upload-process">上传中...</div>');
                                }else{
                                    $("#js_img_err").html("请选择图片文件(jpg|jpeg|png)").show();
                                }
                            },
                            done: function (e, data) {
                                $("#img_wrap").find(".upload-process").remove();
                                var res = data.result;
                                if(res.retCode == "0"){
                                    var img_url = res.data.path;
                                    $("#img_wrap").html('<img src="' + img_url + '" />');
                                    $("#img_url").val(img_url);
                                    $("#js_img_err").html("");
                                }else{
                                    $("#js_img_err").html("上传失败:" + res.errMsg).show();
                                }
                            }
                        });
                    },
                    ok:function(){
                        var img_url = $("#img_url").val();
                        if(!img_url){
                            $("#js_img_err").html("请选择上传的图片");
                            return false;
                        }
                        $("#js_img_err").html("");
                        _this.saveNavImg(oid,nav_name,img_url);
                        return true;
                    },
                    cancel:function(){
                    }
                }).showModal();
            });
        },
        saveNavImg:function(oid,nav_name,img_url){
            var url = api_prefix + "save-nav-img";
            var data = {
                token:this.token,
                oid:oid,
                nav_name:nav_name,
                img_url:img_url
            };
            util.myAjax({
                url: url,
                type: "POST",
                context: this,
                data: data,
                success: function(json) {
                    if (json.retCode == 0) {
                        this.getNavSettings();
                    }else{
                        ui.showErrTip(json.errMsg);
                    }
                }
            });
        },
        //切换风格
        changeStyle:function(style_code){
            var url = api_prefix + "change-style";
            var data = {
                token:this.token,
                style_code:style_code
            };
            util.myAjax({
                url: url,
                type: "POST",
                context: this,
                data: data,
                success: function(json) {
                    if (json.retCode == 0) {
                        ui.showSucTip();
                        this.setCurStyle(style_code);
                        this.getNavSettings();
                    }else{
                        ui.showErrTip(json.errMsg);
                    }
                }
            });
        },
        setCurStyle:function(style_code){
            this.styleCode = style_code;
            $("#radio_list input:radio[value='" + style_code + "']").prop("checked",true);
            $("#style_list li>a").removeClass("selected").filter(function(){
                return $(this).attr("style-code") == style_code;
            }).addClass("selected");
        },
        resetNavImg:function(oid){
            var url = api_prefix + "reset-nav-img";
            var data = {
                token:this.token,
                nav_setting_id:oid
            };
            util.myAjax({
                url: url,
                type: "POST",
                context: this,
                data: data,
                success: function(json) {
                    if (json.retCode == 0) {
                        ui.showSucTip();
                        this.getNavSettings();
                    }else{
                        ui.showErrTip(json.errMsg);
                    }
                }
            });
        },
        getCurStyle:function(){
            var url = api_prefix + "get-cur-style";
            var data = {
                token:this.token
            };
            util.myAjax({
                url: url,
                type: "GET",
                context: this,
                data: data,
                success: function(json) {
                    ui.removeLoading();
                    if (json.retCode == 0) {
                        var style_code = json.data ? json.data.style_code : "default"; //没有指定风格时，设置默认值
                        this.setCurStyle(style_code);
                        this.getNavSettings();
                    }else{
                        ui.showErrTip(json.errMsg);
                    }
                }
            });
            ui.loading();
        },
        getNavSettings:function(){
            var url = api_prefix + "get-nav-settings";
            var data = {
                token:this.token,
                style_code:this.styleCode
            };
            util.myAjax({
                url: url,
                type: "GET",
                context: this,
                data: data,
                success: function(json) {
                    if (json.retCode == 0) {
                        this.bindNavSettings(json.data);
                    }else{
                        util.alert("加载导航列表失败：" + json.errMsg);
                    }
                }
            });
        },
        bindNavSettings:function(list){
            var html = tmpl('row_tmpl', {list:list});
            $("#nav_setting_tbl").html(html);
        }
    };
    module.exports = StyleSetting;
});